What is @thi.ng/errors?
@thi.ng/errors is a utility library for creating and handling custom error types in JavaScript. It provides a set of predefined error classes and functions to simplify error handling and improve code readability.
What are @thi.ng/errors's main functionalities?
Custom Error Classes
This feature allows you to create and use custom error classes like IllegalArgumentError and IllegalStateError to handle specific error conditions in your code.
const { IllegalArgumentError, IllegalStateError } = require('@thi.ng/errors');
function checkAge(age) {
if (age < 0) {
throw new IllegalArgumentError('Age cannot be negative');
}
if (age > 120) {
throw new IllegalStateError('Age seems unrealistic');
}
return 'Age is valid';
}
try {
console.log(checkAge(-5));
} catch (e) {
console.error(e.message);
}
Predefined Error Types
The package provides predefined error types like UnsupportedOperationError to handle common error scenarios in a standardized way.
const { UnsupportedOperationError } = require('@thi.ng/errors');
function notImplemented() {
throw new UnsupportedOperationError('This operation is not supported');
}
try {
notImplemented();
} catch (e) {
console.error(e.message);
}
Error Wrapping
The wrapError function allows you to wrap existing errors with additional context, making it easier to debug and understand the root cause of errors.
const { wrapError } = require('@thi.ng/errors');
function riskyOperation() {
try {
throw new Error('Something went wrong');
} catch (e) {
throw wrapError(e, 'Risky operation failed');
}
}
try {
riskyOperation();
} catch (e) {
console.error(e.message);
}
Other packages similar to @thi.ng/errors
create-error-class
create-error-class is a utility for creating custom error classes in JavaScript. It is similar to @thi.ng/errors in that it allows for the creation of custom error types, but it does not provide predefined error types or error wrapping functionality.
verror
verror is a library for creating and manipulating chained errors. It provides more advanced error wrapping and chaining capabilities compared to @thi.ng/errors, making it suitable for complex error handling scenarios.
custom-error-generator
custom-error-generator is a simple utility for generating custom error classes. It is similar to @thi.ng/errors in terms of creating custom error types but lacks the predefined error types and error wrapping features.
This project is part of the
@thi.ng/umbrella monorepo.
About
Custom error types and error factory functions.
Additional error types can be defined using
defError()
.
Status
STABLE - used in production
Search or submit any issues for this package
Installation
yarn add @thi.ng/errors
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/errors?module" crossorigin></script>
// UMD
<script src="https://unpkg.com/@thi.ng/errors/lib/index.umd.js" crossorigin></script>
Package sizes (gzipped, pre-treeshake): ESM: 356 bytes / CJS: 448 bytes / UMD: 508 bytes
Dependencies
None
API
Generated API docs
import * as err from "@thi.ng/errors";
err.illegalArity(3)
err.illegalArgs("expected foo");
err.illegalState("oops");
err.unsupported("TODO not yet implemented")
const MyError = err.defError(
() => "Eeek... ",
(x) => x + " is not allowed!"
);
try {
throw new MyError(23);
} catch(e) {
console.warn(e.message);
console.log(e instanceof Error);
}
Authors
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-errors,
title = "@thi.ng/errors",
author = "Karsten Schmidt",
note = "https://thi.ng/errors",
year = 2018
}
License
© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0